package com.zt.config;

import com.zt.job.JobDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {
    @Autowired
    private JobDemo jobDemo;
    //1、job：做什么事
    //Map<methodInvokingJobDetailFactoryBean, new MethodInvokingJobDetailFactoryBean>
    @Bean
    public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(JobDemo jobDemo){
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean=new MethodInvokingJobDetailFactoryBean();
        jobDetailFactoryBean.setTargetObject(jobDemo);
        jobDetailFactoryBean.setTargetMethod("printTime");
        return jobDetailFactoryBean;
    }
    //2、trigger：什么时候做
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(MethodInvokingJobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean triggerFactoryBean=new CronTriggerFactoryBean();
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        triggerFactoryBean.setCronExpression("*/5 * * * * ?");
        return triggerFactoryBean;
    }
    //3、scheduler：什么时候做什么事
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean triggerFactoryBean){
        SchedulerFactoryBean factoryBean=new SchedulerFactoryBean();
        factoryBean.setTriggers(triggerFactoryBean.getObject());
        return factoryBean;
    }
}
